<template>
	<view class="newItem" @click="toInfo">
		<view class="userData">
			<image :src="newData.userRsp.avatar?newData.userRsp.avatar:'/static/images/logo.png'" mode="aspectFill"
				@click.stop="toMy()"></image>

			<view class="title" v-if="isfirst">
				{{ newData.userRsp.nickname?newData.userRsp.nickname:newData.userRsp.username?newData.userRsp.username:newData.userRsp.realname}}
			</view>
			<view class="title" v-else>
				{{ newData.userRsp.realname?newData.userRsp.realname:newData.userRsp.nickname?newData.userRsp.nickname:newData.userRsp.username}}
			</view>
			<view class="desc">{{ newData.postTime }}</view>
		</view>


		<view class="main">
			<view class="box">
				<view class="textbox">
					<view class="text" v-if="newData.title">{{ newData.title }}</view>
					<view class="descs" v-if="(newData.content) && (!newData.title)">{{ newData.content }}</view>
				</view>
			</view>
		</view>
		<view class="imglist">
			<image :style="{ width: imageWidth }" @click.stop="toInfo(1)" :key="index2"
				v-for="(item2,index2) in newData.images" :src="item2" mode="widthFix"></image>
		</view>

		<view v-if="newData.videos && newData.videos.length > 0">
			<!-- :poster="newData.posterUrl"  -->
			<!--  object-fit="cover"  -->
			<video :enable-play-gesture="true" :id="'video'+newIndex" @play="videoPlay()"
				:poster="newData.posterUrl" @click.stop style="width: 100%;height: 450px;" :src="newData.videos[0]"
				:autoplay="false"></video>
		</view>


		<view class="iconlist" v-if="myarticle">
			<!-- 这里加一个去转化的按钮 -->
			<view class="li">
				<text class="info-text" :class="newData.auditColor" style="cursor: pointer;">{{newData.auditResult}}</text>
			</view>
			<view class="li" @click.stop="deleteArticle()()">
				<i class="iconfont">&#xe718;</i>
				<text class="info-text  text-danger" style="cursor: pointer;">删除</text>
			</view>
			<view class="li" @click.stop="shareArticle()" v-if="!newData.shareToAll && !newData.systemArticle && newData.auditResult=='审核通过'">
				<i class="iconfont">&#xe681;</i>
				<text class="info-text  text-success" style="cursor: pointer;">分享成公共文章</text>
			</view>
			<view class="li" @click.stop="toInfo()" v-if="isdraft">
				<i class="iconfont">&#xe681;</i>
				<text class="info-text text-success" style="cursor: pointer;">去发布</text>
			</view>
		</view>
		<view class="iconlist" v-else>
			<view class="li" @click.stop="deleteArticle()" v-if="zhuiyi && username == newData.createUsername">
				<i class="iconfont">&#xe718;</i>
				<text class="info-text">删除</text>
			</view>
			<view class="li" @click.stop="deleteArticle()"
				v-if="isMylife && showDelete && username == newData.createUsername">
				<i class="iconfont">&#xe718;</i>
				<text class="info-text">删除</text>
			</view>
			<view class="li" @click.stop="share">
				<i class="iconfont">&#xe681;</i>
				<text class="info-text">分享到社交媒体</text>
			</view>
			<!-- !newData.shareToAll && !newData.systemArticle -->
			<view class="li" @click.stop="shareArticle()"  v-if="!newData.shareToAll && !newData.systemArticle">
				<i class="iconfont">&#xe681;</i>
				<text class="info-text  text-success" style="cursor: pointer;">分享成公共文章</text>
			</view>
			<view class="li" @click.stop="toInfo(1)">
				<i class="iconfont">&#xe67a;</i>
				<text class="info-text">{{ newData.commentCount }}</text>
			</view>
			<view class="li" @click.stop="$u.throttle(fabulous, 1500)" v-if="!isMylife">
				<i class="iconfont iconRed" v-if="newData.myLike">&#xec8c;</i>
				<i class="iconfont" v-else>&#xec7f;</i>
				<text class="info-text">{{ newData.likeCount }} </text>
			</view>

			<view class="li" @click.stop="$u.throttle(myFavorite, 1500)" v-if="!isMylife">
				<i class="iconfont iconRed" v-if="newData.myFavorite">&#xe86d;</i>
				<i class="iconfont" v-else>&#xe8ba;</i>
				<text class="info-text">收藏</text>
			</view>

			<view class="li" v-if="isMylife">
			</view>
		</view>

		<el-dialog title="选择想要分享的文章分类" :visible.sync="chooseLifeCategoryDia" width="40%" style="z-index: 9999;">
			<div>
				<!-- <el-cascader :show-all-levels="false"  :options="articleCategories"></el-cascader> -->
				<el-dropdown @command="changeToArticleCategory">
					<span class="el-dropdown-link">
						文章分类： {{choosedArticleCategory}}
						<i class="el-icon-arrow-down el-icon--right"></i>
					</span>
					<el-dropdown-menu slot="dropdown">
						<el-dropdown-item :command="item" v-for="(item,index) in tabBars" :key="index">{{item.name}}
						</el-dropdown-item>
					</el-dropdown-menu>
				</el-dropdown>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="chooseLifeCategoryDia = false">取 消</el-button>
				<el-button type="primary" @click="band()">确 定</el-button>
			</span>
		</el-dialog>
	</view>
</template>

<script>
	export default {
		components: {

		},
		props: {
			isfirst: {
				type: Boolean,
				default: false
			},
			ispinglun: {
				type: Boolean,
				default: false
			},
			showDelete: {
				type: Boolean,
				default: true
			},
			tabname: {
				type: String,
				default: ''
			},
			// index: 0,
			videoRefPrefix: '',
			myarticle: {
				type: Boolean,
				default: false
			},
			zhuiyi: {
				type: Boolean,
				default: false
			},
			isdraft: {
				type: Boolean,
				default: false
			},
			new: {
				type: Object,
				default: {}
			},
			newIndex: {
				type: Number,
				default: 0
			},
			isMylife: {
				type: Boolean,
				default: false
			}
		},
		watch: {
			new: {
				handler(newVal, oldVal) {
					this.newData = newVal
				},
				deep: true
			}
		},
		data() {
			return {
				videoRefPrefixData: '',
				choosedCategoryId: '',
				choosedArticleCategory: '请选择想要转换的文章分类',
				chooseLifeCategoryDia: false,
				tabBars: [],
				newData: this.new,
				username: uni.getStorageSync('username') ? uni.getStorageSync('username') : ''
			};
		},
		created() {
			this.videoRefPrefixData = this.videoRefPrefix
			let auditResult = this.newData.auditResult
			if (auditResult == 'NO_NEED' || auditResult == 'PASS' ) {
				this.newData.auditResult = '审核通过'
				this.newData.auditColor = 'text-success'
			} else if (auditResult == 'WAIT') {
				this.newData.auditResult = '审核中'
				this.newData.auditColor = 'text-warning'
			} else {
				this.newData.auditResult = '审核不通过'
				this.newData.auditColor = 'text-danger'
			}
		},
		computed: {
			// videoId(){
			// 	if(this.videoRefPrefix){
			// 		return 'video'+this.videoRefPrefix+this.newIndex
			// 	}else{
			// 		return 'video'+this.newIndex
			// 	}
			// },
			// canShare(){
			// 	return (!Object.getOwnPropertyNames(this.newData).includes("shareToAll")) || this.newData.shareToAll==false 
			// },
	
			imageWidth() {
				let imageLength = this.newData.images.length
				if (imageLength == 1) {
					return '500px'
				} else if (imageLength == 2) {
					return '250px'
				} else {
					return '200px'
				}
			}
		},
		methods: {
			changeToArticleCategory(e) {
				console.log('changeToArticleCategory', e);
				this.choosedCategoryId = e.id
				this.choosedArticleCategory = e.name
				this.shareArticle()
			},
			videoPlay() {
				this.$emit('videoPlay', this.newIndex)
			},
			videoPause() {
				let video = uni.createVideoContext("video" + this.newIndex, this)
				// console.log("video" + this.newIndex + ' pause');
				video.pause();
			},
			share() {
				// #ifdef H5
				this.$Show.show('暂不支持')
				// #endif

				// #ifdef MP-WEIXIN
				this.$emit('share', this.newData)
				// #endif
			},
			toMy() {
				console.log('toMy', this.tabname);
				if (this.tabname == '分享') {
					return
				}
				let myFamilyData = uni.getStorageSync('myFamilyData')

				console.log(this.newData)
				console.log(myFamilyData)

				// let id = this.newData.userRsp.docId
				let username = this.newData.userRsp.username ? this.newData.userRsp.username : ''
				if (username) {
					uni.navigateTo({
						url: `/pages/card/index?username=${username}&type=1`
					})
				}

				// if(this.newData.username == myFamilyData.username){
				// 	uni.switchTab({
				// 		url:'/pages/tabbar/mine/mine'
				// 	})
				// }else{
				// 	uni.navigateTo({
				// 		url:`/pages/tabbar/mine/othermine?username=${this.newData.username}`
				// 	})
				// }



			},
			shareArticle() {
				if (this.isMylife) {
					console.log('shareLife', this.newData);
					if (this.choosedCategoryId) {
						uni.showModal({
							title: '提示',
							content: '是否确认分享生活？',
							success: res => {
								if (res.confirm) {
									console.log('share life', this.newData)
									let data = {
										lifeIds: [this.newData.docId],
										targetCategoryId: this.choosedCategoryId
									}
									this.$api.lifeToArticle(data).then(res => {
										this.choosedCategoryId = '';
										this.chooseLifeCategoryDia = false;
										this.$Show.show('生活分享成功');
									});
								}
							}
						});
					} else {
						if (this.tabBars.length == 0) {
							this.$api.getAllCategories().then(res => {
								if (res) {
									for (let i in res) {
										this.tabBars.push({
											name: res[i],
											id: i
										});
									}
								}
							});
						}
						this.chooseLifeCategoryDia = true
					}
					this.$emit('shareLife', this.newData)
				} else {
					uni.showModal({
						title: '提示',
						content: '是否确认分享文章？',
						success: res => {
							if (res.confirm) {
								console.log('share article', this.newData)
								let data = {
									articleIds: [this.newData.docId]
								}
								this.$api.articleToShared(data).then(res => {
									this.$Show.show('文章分享成功');
								});
							}
						}
					});
				}

			},
			deleteArticle() {
				uni.showModal({
					title: '提示',
					content: '是否确认删除？',
					success: res => {
						if (res.confirm) {
							if (this.isMylife) {
								console.log(this.newData)
								let data = {
									lifeId: this.newData.docId
								}
								this.$api.deleteLife(data).then(res => {
									this.$Show.show('删除成功');
									this.$emit('delOver', this.newIndex)
								});
							} else {
								let data = {
									articleId: this.newData.docId
								}
								this.$api.deleteArticle(data).then(res => {
									this.$Show.show('删除成功');
									this.$emit('delOver', this.newIndex)
									uni.setStorageSync('needRefresh', 1)
								});
							}

						}
					}
				});
			},
			toPage(url) {
				console.log(url);
			},
			myFavorite() {
				let data = {
					favorite: true,
					articleId: this.newData.docId
				};
				if (this.newData.myFavorite) {
					data.favorite = false;
				}
				uni.showLoading({
					title: '加载中...'
				});
				this.$api.favoriteUnFavoriteArticle(data).then(res => {
					if (data.favorite) {
						this.$Show.show('收藏成功');
					} else {
						this.$Show.show('取消成功');
						console.log('123');
						//这是为了收藏中心 取消后删除一条
						this.$emit('cancelNew');
					}
					uni.hideLoading();
					this.newData.myFavorite = !this.newData.myFavorite;
					this.$forceUpdate();
				});
			},
			fabulous() {
				let data = {
					like: true,
					articleId: this.newData.docId
				};
				if (this.newData.myLike) {
					data.like = false;
				}
				this.$api.likeUnlikeArticle(data).then(res => {
					if (data.like) {
						this.$Show.show('点赞成功');
						this.newData.likeCount++;
					} else {
						this.newData.likeCount--;
					}

					this.newData.myLike = !this.newData.myLike;
					console.log(this.newData)
					// this.$set(this.newData,"myLike",!this.newData.myLike)
					this.$forceUpdate();
				});
			},
			collection() {

			},
			toInfo(type) {
				console.log('isdraft', this.isdraft)
				console.log('isMylife', this.isMylife)
				console.log('newData', this.newData)
				let data = encodeURIComponent(JSON.stringify(this.newData))
				if (this.isdraft) {
					if (this.isMylife) {
						console.log(this.newData)
						// uni.setStorageSync('life_caogao',data)
						if (this.newData.htmlArticle) {
							//文章
							uni.navigateTo({
								url: `/pages/create/article?isMylife=1&data=${data}`
							});
						} else {
							if (this.newData.videos && this.newData.videos.length > 0) {
								//视频
								uni.navigateTo({
									url: `/pages/create/video?isMylife=1&data=${data}`
								});
							} else {
								//图文
								uni.navigateTo({
									url: `/pages/create/picture?isMylife=1&data=${data}`
								});
							}
						}
					} else {
						console.log('走这里', this.newData)
						// uni.setStorageSync('family_caogao',data)
						if (this.newData.htmlArticle) {
							//文章
							uni.navigateTo({
								url: `/pages/create/article?data=${data}`
							});
						} else {
							if (this.newData.videos && this.newData.videos.length > 0) {
								//视频
								console.log('----------');
								uni.navigateTo({
									url: `/pages/create/video?data=${data}`
								});
							} else {
								//图文
								uni.navigateTo({
									url: `/pages/create/picture?data=${data}`
								});
							}
						}
					}
				} else {
					let url = 'details';
					console.log('go details');
					if (this.isMylife) {
						uni.navigateTo({
							url: `/pages/news/${url}?data=${data}&isMylife=1&ispinglun=${this.ispinglun}`
						});
					} else {
						let id = this.newData.docId;
						let isShared = this.newData.shareToAll || this.newData.systemArticle
						console.log('isShared', isShared);
						uni.navigateTo({
							url: `/pages/news/${url}?data=${data}&type=${type}&id=${id}&ispinglun=${this.ispinglun}&isShared=${isShared}`
						});
					}

				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.newItem {
		background-color: #fff;
		padding: 30rpx;
	}

	.userData {
		position: relative;
		padding-left: 100rpx;
		margin-bottom: 20rpx;

		image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
		}

		.title {
			font-size: 30rpx;
			font-weight: bold;
			margin-bottom: 3px;
		}

		.desc {
			font-size: 24rpx;
			color: #979696;
		}
	}

	.main {
		padding-bottom: 20rpx;

		.title {
			font-size: 32rpx;
			margin-bottom: 20rpx;
			margin-top: 30rpx;
		}

		.imgBox {
			border-radius: 10rpx;
			overflow: hidden;
			position: relative;
			width: 300rpx;
			height: 300rpx;



			.posterUrl {
				border-radius: 10rpx;
				margin-right: 20rpx;
			}

			.bofang {
				position: absolute;
				width: 80rpx;
				left: 50%;
				opacity: .8;
				top: 50%;
				transform: translate(-50%, -50%);
				z-index: 1;
				font-size: 88rpx;
				color: #fff;
			}
		}

		.box {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.textbox {
				display: flex;
				flex-direction: column;
				font-size: 38rpx;

				.descs {
					color: #000;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}
			}

			.text {
				// font-size: 32rpx;
			}

			.image {
				// margin-left: 20rpx;

				// image {
				// 	width: 240rpx;
				// 	height: 150rpx;
				// 	border-radius: 10rpx;
				// }
			}
		}
	}

	.iconlist {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;

		.li {
			display: flex;
			align-items: center;
			flex-direction: row;
			color: #333;

			.iconfont {
				margin-right: 10rpx;
			}

			.iconRed {
				color: #de4849;
			}

			.info-text {
				font-size: 28rpx;
			}
		}
	}

	.wenzibox {
		display: flex;
		flex-direction: column;

		image {
			width: 300rpx;
			border-radius: 10rpx;
			height: 300rpx;
		}

		.posterUrl {
			width: 400rpx;
			height: 260rpx;
			border-radius: 10rpx;
		}
	}

	.imglist {
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		image {
			// width: 200px;
			height: auto;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			margin-left: 10rpx;
		}
	}
</style>